//发布订阅模式
interface I {
    events:Map<string,Function[]>
    once:(event:string, callback:Function)=>void  //触发一次
    on:(event:string, callback:Function)=>void  //订阅
    emit:(event:string, ...args:any[])=>void  //派发
    off:(event:string, callback:Function)=>void  //移除
}
class Emitter implements I {
    events: Map<string, Function[]>
    constructor () {
        this.events = new Map()
    }
    once (event:string, callback:Function) {
        //1.创建一个自定义函数，通过on触发一次，触发完就off掉
        const cb = (...args:any[]) => {
            callback(...args)
            this.off(event, cb)
        }
        this.on(event, cb)
    }
    on (event:string, callback:Function) {
        if (this.events.has(event)) {
            //事件存过了
            const callbackList = this.events.get(event)
            callbackList && callbackList.push(callback)
        } else {
            //事件第一次存
            this.events.set(event,[callback])
        }
    }
    emit (event:string, ...args:any[]) {
        const callbackList = this.events.get(event)
        if (callbackList) {
            callbackList.forEach(fn => {
                fn(...args)
            });
        }
    }
    off (event:string, callback:Function) {
        const callbackList = this.events.get(event)
            if (callbackList) {
                callbackList.splice(callbackList.findIndex(callback), 1)
            }
        }
}

const bus = new Emitter()

bus.on('message', (b:boolean, n:number)=>{

})

bus.emit('message', false, 1)